import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SeatDAO {
    /**
     * 根据放映厅ID获取所有座位
     */
    public static List<Seat> getSeatsByHallId(String hallId) {
        List<Seat> seats = new ArrayList<>();
        String sql = "SELECT * FROM seats WHERE hall_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, hallId);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                Seat seat = new Seat(
                        rs.getString("id"),
                        rs.getString("hall_id"),
                        rs.getInt("row_num"),
                        rs.getInt("col_num")
                );
                seat.setSold(rs.getBoolean("sold"));
                seats.add(seat);
            }
        } catch (SQLException e) {
            System.err.println("查询座位失败: " + e.getMessage());
        }
        return seats;
    }

    /**
     * 检查座位是否可售
     */
    public static boolean isSeatAvailable(String seatId, String showId) {
        // 先检查座位是否存在且未被售出
        String sql = "SELECT sold FROM seats WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, seatId);
            ResultSet rs = pstmt.executeQuery();

            if (!rs.next()) {
                return false; // 座位不存在
            }

            return !rs.getBoolean("sold");
        } catch (SQLException e) {
            System.err.println("检查座位可用性失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 锁定座位（标记为已售出）
     */
    public static void lockSeats(List<String> seatIds) {
        if (seatIds.isEmpty()) return;

        StringBuilder sql = new StringBuilder("UPDATE seats SET sold = true WHERE id IN (");
        for (int i = 0; i < seatIds.size(); i++) {
            if (i > 0) sql.append(",");
            sql.append("?");
        }
        sql.append(")");

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql.toString())) {

            for (int i = 0; i < seatIds.size(); i++) {
                pstmt.setString(i + 1, seatIds.get(i));
            }

            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("锁定座位失败: " + e.getMessage());
            throw new RuntimeException("锁定座位失败", e);
        }
    }

    /**
     * 解锁座位（标记为未售出）
     */
    public static void unlockSeats(List<String> seatIds) {
        if (seatIds.isEmpty()) return;

        StringBuilder sql = new StringBuilder("UPDATE seats SET sold = false WHERE id IN (");
        for (int i = 0; i < seatIds.size(); i++) {
            if (i > 0) sql.append(",");
            sql.append("?");
        }
        sql.append(")");

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql.toString())) {

            for (int i = 0; i < seatIds.size(); i++) {
                pstmt.setString(i + 1, seatIds.get(i));
            }

            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("解锁座位失败: " + e.getMessage());
            throw new RuntimeException("解锁座位失败", e);
        }
    }
}
